<?php
/**
 * Description:
 *
 * @author      Eden
 * @datetime    2017/1/11 00:12
 * @copyright   WuHan HuQi Technology Co.,Ltd.
 * @url         http://cnhuqi.com
 */

class Delete2_Model extends API_Model
{
    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 主程序
     * @param $params
     */
    public function run($params)
    {
        $send['state'] = false;
        do {

            if (empty($params['userId']) || empty($params['sn']) || empty($params['status'])) {
                $send['error'] = '参数错误';
                break;
            }

            $orderDb = clone($this->db);
            $q = $orderDb->where(['uuid' => $params['sn']])->get();
            $rows = $q->result_array();

            if (empty($rows)) {
                $send['error'] = '订单不存在';
                break;
            }

            $db = clone($this->db);
            foreach ($rows as $row) {
                if ($row['status'] != 'Unpaid' || $row['status'] != 'Cancel') {
                    $send['error'] = '订单状态不可更改';
                    break 2;
                }

                $db->where(['id' => $row['orderId']]);
                $db->delete('user_order');
                if (!$db->affected_rows()) {
                    $send['error'] = '订单删除失败';
                    break 2;
                }
            }

            $send['state'] = true;
            $send['data'] = [
                'msg' => '订单删除成功'
            ];

        } while(false);

        $this->output->send($send);
    }
}

